<template>
  <div>
    <el-switch
      v-model="state"
      active-text="开启数据切换"
      inactive-text="关闭数据切换"
      active-color="#13ce66"
      inactive-color="#eee"
      @change="handleChange"
    ></el-switch>
    <div ref="box" class="box"></div>
  </div>
</template>

<script>
// import * as echarts from 'echarts/core'
// import { TooltipComponent } from 'echarts/components'
// import { RadarChart } from 'echarts/charts'
// import { CanvasRenderer } from 'echarts/renderers'

// echarts.use([TooltipComponent, RadarChart, CanvasRenderer])
export default {
  data () {
    return {
      state: false,
      option: {
        tooltip: {},
        radar: {
          // shape: 'circle',
          axisName: {
            // textStyle: {
            color: '#fff',
            backgroundColor: '#999',
            borderRadius: 3,
            padding: [3, 5]
            // }
          },
          indicator: [
            { name: '工作效率', max: 100 },
            { name: '考勤', max: 100 },
            { name: '积极性', max: 100 },
            { name: '帮助同事', max: 100 },
            { name: '自主学习', max: 100 },
            { name: '正确率', max: 100 }
          ]
        },
        series: [
          {
            name: 'Budget vs spending',
            type: 'radar',
            data: [
              {
                value: [50, 40, 60, 50, 50, 40],
                name: '张三'
              },
              {
                value: [50, 50, 50, 50, 50, 30],
                name: '李四'
              }
            ]
          }
        ]
      }
    }
  },
  mounted () {
    this.initChart()
  },
  methods: {
    initChart () {
      this.myChart = this.echarts.init(this.$refs.box)
      this.myChart.setOption(this.option)
    },
    handleChange () {
      if (this.setIntervalId) {
        clearInterval(this.setIntervalId)
        this.setIntervalId = null
        return
      }
      this.setIntervalId = setInterval(this.changeData, 1000)
    },
    changeData () {
      const data = []
      for (let i = 0; i < 3; i++) {
        const person = { value: [], name: `赵${i}` }
        for (let j = 0; j < 6; j++) {
          person.value.push(Math.ceil(Math.random() * 100 + 1))
        }
        data.push(person)
      }
      this.option.series[0].data = data
      this.myChart.setOption(this.option)
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 400px;
}
</style>
